

#include "stdafx.h"
#include "pf_dimradial.h"
//#include "pf_constructionline.h"
#include "pf_text.h"
#include "pf_solid.h"
#include "pf_graphic.h"


/**
 * Constructor.
 *
 * @para parent Parent Entity Container.
 * @para d Common dimension geometrical data.
 * @para ed Extended geometrical data for radial dimension.
 */
PF_DimRadial::PF_DimRadial(PF_Container* parent,
                           const PF_DimensionAttribute& d,
                           const PF_DimRadialAttribute& ed)
        : PF_Dimension(parent, d), edata(ed) {}



/**
 * @return Automatically created label for the default 
 * measurement of this dimension.
 */
QString PF_DimRadial::getMeasuredLabel() {

    // Definitive dimension line:
    double dist = data.definitionPoint.distanceTo(edata.definitionPoint);

    PF_Graphic* graphic = getGraphic();

    QString ret;
    if (graphic!=NULL) {
        ret = PF_Units::formatLinear(dist, graphic->getUnit(),
                                     graphic->getLinearFormat(), graphic->getLinearPrecision());
    } else {
        ret = QString("%1").arg(dist);
    }

    return ret;
}


PF_VectorSolutions PF_DimRadial::getRefPoints() {
	PF_VectorSolutions ret(edata.definitionPoint,
						data.definitionPoint, data.middleOfText);
	return ret;
}


/**
 * Updates the sub entities of this dimension. Called when the 
 * dimension or the position, alignment, .. changes.
 *
 * @param autoText Automatically reposition the text label
 */
void PF_DimRadial::update(bool autoText) {

    PF_DEBUG->print("PF_DimRadial::update");

    clear();

    if (isUndone()) {
        return;
    }

    // dimension line:
    //updateCreateDimensionLine(data.definitionPoint, edata.definitionPoint,
    //false, true);

    PF_Vector p1 = data.definitionPoint;
    PF_Vector p2 = edata.definitionPoint;
    double angle = p1.angleTo(p2);

    // text height (DIMTXT)
    double dimtxt = getTextHeight();
    // text distance to line (DIMGAP)
    double dimgap = getDimensionLineGap();

    // length of dimension line:
    double length = p1.distanceTo(p2);

    PF_TextAttribute textData;

    textData = PF_TextAttribute(PF_Vector(0.0,0.0),
                           dimtxt, 30.0,
                           LX_Define::VAlignMiddle,
                           LX_Define::HAlignCenter,
                           LX_Define::LeftToRight,
                           LX_Define::Exact,
                           1.0,
                           getLabel(),
                           "standard",
                           0.0);

    PF_Text* text = new PF_Text(this, textData);
    double textWidth = text->getSize().x;

    // do we have to put the arrow / text outside of the arc?
    bool outsideArrow = (length<getArrowSize()*2+textWidth);
    double arrowAngle;

    if (outsideArrow) {
        length += getArrowSize()*2 + textWidth;
        arrowAngle = angle+M_PI;
    } else {
        arrowAngle = angle;
    }

    // create arrow:
    PF_SolidAttribute sd;
    PF_Solid* arrow;

    arrow = new PF_Solid(this, sd);
    arrow->shapeArrow(p2,
                      arrowAngle,
                      getArrowSize());
    arrow->setPen(PF_Pen(LX_Define::FlagInvalid));
    arrow->setLayer(NULL);
    addEntity(arrow);

    PF_Vector p3;
    p3.setPolar(length, angle);
    p3 += p1;

    // Create dimension line:
    PF_Line* dimensionLine = new PF_Line(this, PF_LineAttribute(p1, p3));
    dimensionLine->setPen(PF_Pen(LX_Define::FlagInvalid));
    dimensionLine->setLayer(NULL);
    addEntity(dimensionLine);

    PF_Vector distV;
    double textAngle;

    // rotate text so it's readable from the bottom or right (ISO)
    // quadrant 1 & 4
    if (angle>M_PI/2.0*3.0+0.001 ||
            angle<M_PI/2.0+0.001) {

        distV.setPolar(dimgap + dimtxt/2.0, angle+M_PI/2.0);
        textAngle = angle;
    }
    // quadrant 2 & 3
    else {
        distV.setPolar(dimgap + dimtxt/2.0, angle-M_PI/2.0);
        textAngle = angle+M_PI;
    }

    // move text label:
    PF_Vector textPos;

    if (data.middleOfText.valid && !autoText) {
        textPos = data.middleOfText;
    } else {
        if (outsideArrow) {
            textPos.setPolar(length-textWidth/2.0-getArrowSize(), angle);
        } else {
            textPos.setPolar(length/2.0, angle);
        }
        textPos+=p1;
        // move text away from dimension line:
        textPos += distV;
    	data.middleOfText = textPos;
    }

    text->rotate(PF_Vector(0.0,0.0), textAngle);
    text->move(textPos);

    text->setPen(PF_Pen(LX_Define::FlagInvalid));
    text->setLayer(NULL);
    addEntity(text);

    calculateBorders();
}



void PF_DimRadial::move(PF_Vector offset) {
    PF_Dimension::move(offset);

    edata.definitionPoint.move(offset);
    update();
}



void PF_DimRadial::rotate(PF_Vector center, double angle) {
    PF_Dimension::rotate(center, angle);

    edata.definitionPoint.rotate(center, angle);
    update();
}



void PF_DimRadial::scale(PF_Vector center, PF_Vector factor) {
    PF_Dimension::scale(center, factor);

    edata.definitionPoint.scale(center, factor);
    edata.leader*=factor.x;
    update();
}



void PF_DimRadial::mirror(PF_Vector axisPoint1, PF_Vector axisPoint2) {
    PF_Dimension::mirror(axisPoint1, axisPoint2);

    edata.definitionPoint.mirror(axisPoint1, axisPoint2);
    update();
}


void PF_DimRadial::moveRef(const PF_Vector& ref, const PF_Vector& offset) {

    if (ref.distanceTo(edata.definitionPoint)<1.0e-4) {
		double d = data.definitionPoint.distanceTo(edata.definitionPoint);
		double a = data.definitionPoint.angleTo(edata.definitionPoint + offset);
		
		PF_Vector v;
		v.setPolar(d, a);
        edata.definitionPoint = data.definitionPoint + v;
		update(true);
    }
	else if (ref.distanceTo(data.middleOfText)<1.0e-4) {
        data.middleOfText.move(offset);
		update(false);
    }
}

/**
 * Dumps the point's data to stdout.
 */
std::ostream& operator << (std::ostream& os, const PF_DimRadial& d) {
    os << " DimRadial: " << d.getData() << "\n" << d.getEData() << "\n";
    return os;
}


